Test Failed
Push — master ( cf7a15...101480 )
by Alexey
05:05
created

inji.Ecommerce.toggleFav   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 11
rs 9.4285
c 2
b 0
f 1
cc 1
nc 1
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 6 1
1
/**
2
 * Ecommerce Classes
3
 */
4
inji.Ecommerce = {
5
  Cart: new function () {
6
    this.addItem = function (itemOfferPriceId, count) {
7
      inji.Server.request({
8
        url: 'ecommerce/cart/add',
9
        data: {
10
          itemOfferPriceId: itemOfferPriceId,
11
          count: count,
12
        },
13
        success: function (data) {
14
          console.log(data);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
15
          inji.Server.request({
16
            url: 'ecommerce/cart/getCart',
17
            success: function (data) {
18
              $("#cart,.cartplace").html(data);
19
            }
20
          });
21
        }
22
      });
23
    }
24
    this.calcSum = function () {
25
      var form = $('.ecommerce .cart-order_page form');
26
      var formData = new FormData(form[0]);
27
      $('.ecommerce .cart-order_page').prepend($('<div style = "position:absolute;width:' + $('.ecommerce .cart-order_page').width() + 'px;height:' + $('.ecommerce .cart-order_page').height() + 'px;background-color: rgba(255, 255, 255, 0.4);z-index:1000000"></div>'));
28
      inji.Server.request({
29
        url: form.attr('action'),
30
        type: 'POST',
31
        data: formData,
32
        dataType: 'html',
33
        processData: false,
34
        success: function (data) {
35
          $('.ecommerce .cart-order_page').html($(data).find('.ecommerce .cart-order_page').html());
36
        }
37
      });
38
    }
39
    this.delItem = function (cart_item_id) {
40
      var form = $('.ecommerce .cart-order_page form');
41
      $('.cart_item_id' + cart_item_id).remove();
42
      var formData = new FormData(form[0]);
43
      $('.ecommerce .cart-order_page').prepend($('<div style = "position:absolute;width:' + $('.ecommerce .cart-order_page').width() + 'px;height:' + $('.ecommerce .cart-order_page').height() + 'px;background-color: rgba(255, 255, 255, 0.4);z-index:1000000"></div>'));
44
      inji.Server.request({
45
        url: form.attr('action'),
46
        type: 'POST',
47
        data: formData,
48
        dataType: 'html',
49
        processData: false,
50
        success: function (data) {
51
          $('.ecommerce .cart-order_page').html($(data).find('.ecommerce .cart-order_page').html());
52
        }
53
      });
54
    }
55
    this.delItemWidget = function (cart_item_id, callback) {
56
      inji.Server.request({
57
        url: '/ecommerce/cart/deleteItem?cartItemId=' + cart_item_id,
58
        success: function (data) {
59
          $("#cart,.cartplace").html(data);
60
          if (callback !== undefined) {
61
            callback();
62
          }
63
        }
64
      });
65
    }
66
  },
67
  toggleFav: function (itemId, btn) {
68
    inji.Server.request({
69
      url: 'ecommerce/toggleFav/' + itemId,
70
      success: function (data) {
71
        $('.ecommerce-favorite-count').html(data.count);
72
        setTimeout(function () {
73
          $(btn).html(data.newText);
74
        }, 100)
75
      }
76
    }, btn);
77
  }
78
}
79
inji.onLoad(function () {
80
81
  //plugin bootstrap minus and plus
82
  //http://jsfiddle.net/laelitenetwork/puJ6G/
83
  $('body').on('click', '.btn-number', function (e) {
84
    e.preventDefault();
85
86
    var fieldName = $(this).data('field');
87
    var type = $(this).data('type');
88
    var input = $("input[name='" + fieldName + "']");
89
    var currentVal = parseFloat(input.val());
90
    if (!isNaN(currentVal)) {
91
      if (type == 'minus') {
92
93
        if (currentVal > input.attr('min')) {
94
          input.val(currentVal - 1).change();
95
        }
96
        if (parseFloat(input.val()) == input.attr('min')) {
97
          $(this).attr('disabled', true);
98
        }
99
100
      } else if (type == 'plus') {
101
102
        if (currentVal < input.attr('max')) {
103
          input.val(currentVal + 1).change();
104
        }
105
        if (parseFloat(input.val()) == input.attr('max')) {
106
          $(this).attr('disabled', true);
107
        }
108
109
      }
110
    } else {
111
      input.val(0);
112
    }
113
  });
114
  $('body').on('focusin', '.input-number', function () {
115
    $(this).data('oldValue', $(this).val());
116
  });
117
  $('body').on('change', '.input-number', function () {
118
119
    var minValue = parseFloat($(this).attr('min'));
120
    var maxValue = parseFloat($(this).attr('max'));
121
    var valueCurrent = parseFloat($(this).val());
122
123
    var name = $(this).attr('name');
124
    if (valueCurrent >= minValue) {
125
      $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled')
126
    } else {
127
      alert('Нельзя заказать меньше ' + minValue);
128
      $(this).val($(this).data('oldValue'));
129
    }
130
    if (valueCurrent <= maxValue) {
131
      $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled')
132
    } else {
133
      alert('Извините, но больше нету');
134
      $(this).val($(this).data('oldValue'));
135
    }
136
137
138
  });
139
140
  $('body').on('keydown', ".input-number", function (e) {
141
    // Allow: backspace, delete, tab, escape, enter and .
142
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
143
      // Allow: Ctrl+A
144
      (e.keyCode == 65 && e.ctrlKey === true) ||
145
      // Allow: home, end, left, right
146
      (e.keyCode >= 35 && e.keyCode <= 39)) {
147
      // let it happen, don't do anything
148
      return;
149
    }
150
    // Ensure that it is a number and stop the keypress
151
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
152
      e.preventDefault();
153
    }
154
  });
155
156
})
157